package com.huya.k12.translate.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huya.k12.translate.model.entity.Word;
import com.huya.k12.translate.service.SVNService;
import com.huya.k12.translate.service.WordService;
import com.huya.k12.translate.util.SVNUtil;
import com.huya.k12.translate.util.XMLUtil;

@Service
public class SVNServiceImpl implements SVNService{
	
	@Autowired
	private SVNUtil svnUtil;
	
	@Autowired
	private WordService wordService;
	
	@Autowired
	private XMLUtil xmlUtil;
	
	private List<Word> addWord;
	private List<Word> updateWord;
	private List<Word> deleteWord;
	
	private Logger logger = Logger.getLogger(SVNServiceImpl.class);

	@Override
	public void commit() {
		// TODO Auto-generated method stub
	//	update();
		List<Word> wordChangeList  = wordService.listByStatus(1);
		try{
			xmlUtil.nodifyDoc(wordChangeList);
		}catch(Exception e){
			logger.error("commit error :"+e.getMessage());
			return;
		}
		wordService.resetStatus();
		List<File> newFileList = xmlUtil.getNewFileList();
		if(newFileList!=null&&newFileList.size()>0){
			for(File file: newFileList)
				svnUtil.addEntry(file);
		}
		svnUtil.commit();
	}

	@Override
	public void update() {
		// TODO Auto-generated method stub
		svnUtil.update();
		updateDB();
	}
	
	private Map<String, Word> dbListToMap(){
		Map<String, Word> map = new HashMap<String, Word>();
		List<Word> wordList = wordService.listAll();
		if(wordList!=null && wordList.size() > 0 ){
			for(Word word : wordList){
				map.put(word.getWordId(), word);
			}
		}
		return map;
	}
	private void updateDB(){
		Map<String,Word> xmlMap = xmlUtil.defaultXMLToMap();
		Map<String,Word> dbMap = dbListToMap();
		addWord = new ArrayList<Word>();
		updateWord = new ArrayList<Word>();
		deleteWord = new ArrayList<Word>();
		for(String key : xmlMap.keySet()){
			Word word= dbMap.get(key);
			if(word == null){
				addWord.add(xmlMap.get(key));
				logger.info("Android add word:"+xmlMap.get(key));
			}
			else if(StringUtils.isNotBlank(xmlMap.get(key).getEn())){
				if(StringUtils.isBlank(word.getEn())){
					word.setEn(xmlMap.get(key).getEn());
					logger.info("Android update word："+xmlMap.get(key));
					updateWord.add(word);
				}
				else if(!word.getEn().equals(xmlMap.get(key).getEn())){
					word.setEn(xmlMap.get(key).getEn());
					logger.info("Android update word："+xmlMap.get(key));
					updateWord.add(word);
				}
			}
		}
		for(String key : dbMap.keySet()){
			Word word = xmlMap.get(key);
			if(word == null){
				deleteWord.add(dbMap.get(key));
				logger.info("Android delete word："+ dbMap.get(key));
			}
		}
		if(addWord != null && addWord.size()>0)
			wordService.addWord(addWord);
		if(updateWord != null && updateWord.size() > 0)
			wordService.updateWord(updateWord);
		if(deleteWord != null && deleteWord.size() > 0)
			wordService.deleteWord(deleteWord);
 	}
	

}
